#include <zmq.h>
#include <string>
#include <iostream>
#include <cstring>

int main()
{
    auto ctx = zmq_ctx_new();
    auto s = zmq_socket(ctx, ZMQ_PAIR);
    zmq_bind(s, "tcp://*:8888");

    // 接收方式一
    {
        char buf[5] = {'\0'};
        int rc = zmq_recv(s, buf, 4, 0);
        std::cout << buf << std::endl;
    }

    // 接收方式二
    {
        zmq_msg_t msg;
        zmq_msg_init(&msg);
        int rc = zmq_msg_recv(&msg, s, 0);
        char *buf = new char[rc + 1];
        memcpy(buf, zmq_msg_data(&msg), rc);
        buf[rc] = '\0';
        std::cout << buf << std::endl;
    }

    zmq_close(s);
    zmq_ctx_destroy(ctx);
    return 0;
}